package com.hzhc.controller.yworder;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.CntrInfo;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.CntrInfoService;
import com.hzhc.service.CntrLogService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.CntrInfoVo;
import com.hzhc.vo.CntrLogVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 集装箱信息 前端控制器
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/cntrInfo")
@RequiredArgsConstructor
public class CntrInfoController extends BaseController {

    private final CntrInfoService infoService;

    private final CntrLogService logService;


    /**
     * 集装箱列表 list(在场箱查询)
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getYzsAllList")
    @ResponseBody
    public ResultUtil<List<CntrInfoVo>> getYzsAllList(HttpServletRequest request, @RequestBody CntrInfoVo vo){
        UserDto userMember = TokenUtil.getUserMember(request);
        if (userMember != null) {
            vo.setCompanyId(userMember.getCompanyId());
            vo.setCompanyName(userMember.getCompanyName());
        }
        Page<CntrInfoVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<CntrInfoVo> list = infoService.getAllYzsList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 箱动态详情
     * @param vo
     * @return
     */
    @RequestMapping("/getYzsCntrLogsList")
    @ResponseBody
    public ResultUtil<List<CntrLogVo>> getYzsCntrLogsList(HttpServletRequest request,@RequestBody CntrLogVo vo) {
        UserDto userMember = TokenUtil.getUserMember(request);
        if (userMember != null) {
            vo.setCompanyId(userMember.getCompanyId());
        }
        Page<CntrLogVo> pagination = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<CntrLogVo> cntrLogVos = logService.getYzsCntrLogsList(vo, pagination);
        return ResultUtil.build(cntrLogVos, pagination.getTotal(),pagination.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 箱号 check
     * @param request
     * @param info
     * @return
     */
    @PostMapping("/checkCntrNo")
    public ResultUtil<T> checkCntrNo(HttpServletRequest request,@RequestBody CntrInfo info){
        Boolean b = infoService.checkCntrNo(info);
        if (b){
            return ResultUtil.ok();
        }
        return ResultUtil.build(ResultUtilCodeEnum.CNTR_NO_EXIST);
    }

    /**
     * 阳逻港铁水联运电子库场图各区箱子计数
     */
    @RequestMapping("/getYardAreaCount")
    public ResultUtil<JSONObject> getYardAreaCount(@RequestBody CntrInfo info) {
        JSONObject list = infoService.getCntrInfo(info);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }
}
